package com.sany.taskmanager.util;

import android.content.Context;
import android.content.SharedPreferences;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * SharedPreferences工具类
 */
public class PreferenceUtil {
    private final static String SHARED_NAME = "sany_crane_boom_task";
    private SharedPreferences preferences;

    private volatile static PreferenceUtil instance = null;

    public static void init(Context context) {
        if (instance == null) {
            synchronized (PreferenceUtil.class) {
                if (instance == null) {
                    instance = new PreferenceUtil(context);
                }
            }
        }
    }

    private PreferenceUtil(Context context) {
        if (preferences == null)
            preferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
    }

    public static PreferenceUtil getPreferences() {
        return instance;
    }

    public synchronized void remove(final String key) {
        preferences.edit().remove(key).apply();
    }

    public synchronized boolean contains(String key) {
        return preferences.contains(key);
    }

    public synchronized boolean getBoolean(final String key, final boolean defaultValue) {
        return preferences.getBoolean(key, defaultValue);
    }

    public synchronized boolean getBoolean(final String key) {
        return getBoolean(key, false);
    }

    public synchronized void putBoolean(final String key, final boolean value) {
        SharedPreferences.Editor preferencesEditor = preferences.edit();
        preferencesEditor.putBoolean(key, value);
        preferencesEditor.apply();
    }

    public synchronized int getInt(final String key, final int defaultValue) {
        return preferences.getInt(key, defaultValue);
    }

    public synchronized int getInt(final String key) {
        return getInt(key, 0);
    }

    public synchronized void putInt(final String key, final int value) {
        SharedPreferences.Editor preferencesEditor = preferences.edit();
        preferencesEditor.putInt(key, value);
        preferencesEditor.apply();
    }

    public synchronized String getString(final String key, final String defaultValue) {
        return preferences.getString(key, defaultValue);
    }

    public synchronized String getString(final String key) {
        return getString(key, "");
    }

    public synchronized void putString(final String key, final String value) {
        SharedPreferences.Editor preferencesEditor = preferences.edit();
        preferencesEditor.putString(key, value);
        preferencesEditor.apply();

    }

    public JSONObject getJSONObject(final String key, final JSONObject defaultValue) {
        JSONObject returnedValue;
        try {
            String jsonString = preferences.getString(key, "");
            returnedValue = (!jsonString.isEmpty() ? new JSONObject(jsonString) : null);
        } catch (Exception e) {
            returnedValue = defaultValue;
        }
        return returnedValue;
    }

    public synchronized JSONObject getJSONObject(final String key) {
        return getJSONObject(key, null);
    }

    public void putJSONObject(final String key, JSONObject jsonObject) {
        putString(key, jsonObject.toString());
    }

    public JSONArray getJSONArray(final String key, final JSONArray defaultValue) {
        JSONArray returnedValue;
        try {
            String jsonString = preferences.getString(key, "");
            returnedValue = (!jsonString.isEmpty() ? new JSONArray(jsonString) : null);
        } catch (Exception e) {
            returnedValue = defaultValue;
        }
        return returnedValue;
    }

    public synchronized JSONArray getJSONArray(final String key) {
        return getJSONArray(key, null);
    }

    public void putJSONArray(final String key, final JSONArray jsonArray) {
        putString(key, jsonArray.toString());
    }
}
